﻿using System;
using System.Linq;
using System.Management;

namespace ClusterAwareProperties
{
    ///<summary>
    /// provider Access to PrivateProperties
    ///</summary>
    public class PrivatePropertiesData : IDisposable
    {
        #region private property

        private readonly ManagementBaseObject _classInstance;

        #endregion

        #region ctor


        /// <summary>
        /// Constructor
        /// </summary>
        private PrivatePropertiesData()
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="queryobj">ManagementObject instance</param>
        public PrivatePropertiesData(ManagementObject queryobj)
            : this()
        {
            try
            {
                _classInstance = (ManagementBaseObject)queryobj["PrivateProperties"];
                PropertyData = _classInstance.Properties.Cast<PropertyData>().ToArray();
                if (PropertyData.Count() == 0)
                {
                    PropertyData = null;
                }
            }
            catch
            {

                PropertyData = null;
            }
        }

        #endregion

        #region public properties
         
        /// <summary>
        /// Represents information about a WMI PrivatesPropertiesM Data.
        /// </summary>
        public PropertyData[] PropertyData { get; internal set; }

        ///<summary>
        /// Return ManagementBaseObject instance of PrivateProperties
        ///</summary>
        public ManagementBaseObject Instance
        {
            get { return _classInstance; }
        }

        #endregion

        /// <summary>
        /// Set Value to Property
        /// </summary>
        /// <param name="nameProperty">Name of property</param>
        /// <param name="value">value to set</param>
        public void SetPropertyValue(string nameProperty,object value)
        {
            foreach (var prop in PropertyData.Where(prop => prop.Name.ToLower() == nameProperty.ToLower()))
            {
                prop.Value = value;
            }
        }

        /// <summary>
        /// Dispose Class
        /// </summary>
        public void Dispose()
        {
            if (Instance != null)
            {
                Instance.Dispose();
            }
        }
    }

    ///<summary>
    /// Convert a PrivateProperties property to  ManagementBaseObject
    ///</summary>
    public static class PrivatePropertiesDataExtender
    {
        ///<summary>
        /// Convert Resource Privateproperties property to ManagementBaseObject
        ///</summary>
        ///<param name="clsprop">Resource Privateproperties property to converter</param>
        /// <returns>
        /// ManagementBaseObject
        /// </returns>
        public static ManagementBaseObject ConvertToManagemenBasetObject(this PrivatePropertiesData clsprop)
        {
            var result = clsprop.Instance;
            if (clsprop.PropertyData != null)
            {
                foreach (var data in clsprop.PropertyData)
                {
                    result[data.Name] = data.Value;
                }
            }
            return result;
        }
    }
}
